import Vue from 'vue'
import Router from 'vue-router'
const login = () => import('../page/Login.vue')
const subMain = () => import('../components/monitor/subMain.vue')
const main = () => import('../components/monitor/main')
const self = () => import('../components/self/self')
const explore = () => import('../components/report/explore')
Vue.use(Router)
const router =  new Router({
  routes: [
    {path: '/',name: "main",redirect : '/main'},
    {path: '/main',name: "main",component:main , meta: { requireAuth: true}},
    {path: '/main/subMain/:parentId',name: "",component:subMain, meta: { requireAuth: true}},
    {path: '/explore',name: "explore",component:explore, meta: { requireAuth: true}},
    {path: '/self',name: "self",component:self, meta: { requireAuth: true}},
    {path: '/login',name: "login",components:{"subPage":login}},
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (window.localStorage.getItem('userName')) {  // 通过vuex state获取当前的token是否存在
      next();
    }else {
      next({ path: '/login' })
    }
  }else {
    next();
  }
})
export default router
